<template>
  <div class="app-container">

    <div class="button" style="padding-bottom:0.5rem">
      <div class="grid-content bg-purple-dark">
        <el-row>
          <el-button type="primary" @click="addDep">添加部门</el-button>
        </el-row>
      </div>
    </div>

    <el-table
      element-loading-text="Loading"
      :data="departmentList"
      border
      fit
      style="width: 100%"
      highlight-current-row
    >
      <el-table-column label="部门编号" align="center">
        <template slot-scope="scope">{{ scope.row.depCode }}</template>
      </el-table-column>
      <el-table-column label="部门名称" align="center">
        <template slot-scope="scope">{{ scope.row.depName }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="editDep(scope.row.depCode)">编辑</el-button>
          <el-button type="danger" size="small" @click="depDel(scope.row.depCode)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      align="center"
    ></el-pagination>
  </div>
</template>

<script>
  import $ from "@/api/departStaff"

  export default {
    data() {
      return {
        currentPage: 1,
        pageSize: 10,
        total: 0,
        departmentList: [],
      }
    },
    created() {
      // 部门列表
      this.fetchData()
    },
    methods: {
      fetchData() {
        this.currentPage = 1
        $.tbDmList({
          pageIndex: this.currentPage,
          pageSize: this.pageSize
        }).then(res => {
          this.departmentList = res.data.list
          this.total = res.data.total;
          // for (var i = 0; i < this.administratorList.length; i++) {
          //   this.$set(this.administratorList[i], "a", i + 1);
          // }
          // console.log(res.data)
        })
      },
      addDep() {
        this.$router.push({
          path: "add"
        });
      },
      editDep(id) {
        this.$router.push({
          path: "edit",
          query:{id:id}
        });
      },
      //删除部门
      depDel(id) {
        this.$confirm("此操作将删除该部门, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            $.depUserList({depCode: id}).then(response => {
              if (response.success) {
                // console.log(response.data.list.length)
                if (response.data.list.length== 0) {
                  // console.log("lll")
                  $.tbDmDeleteTrue({depCode: id}).then(response => {
                    this.$message({
                      type: "success",
                      message: "删除成功!"
                    });
                    this.fetchData();
                  });
                } else {
                  this.$message({
                    type: "error",
                    message: "该部门中还有成员,请清空后再删除!"
                  });
                }
              }
            });
            //console.log(id)
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },

      // 翻页查看
      find(pageIndex) {
        $.tbDmList({
          pageIndex: pageIndex,
          pageSize: this.pageSize
        }).then(res => {
          this.departmentList = res.data.list
          this.total = res.data.total;
          // for (var i = 0; i < this.administratorList.length; i++) {
          //   this.$set(this.administratorList[i], "a", i + 1);
          // }

        })
      },

      handleSizeChange(val) {
        this.pageSize = val;
        this.currentPage = 1;
        if (this.state == 1) {
          this.find(this.currentPage)
        } else this.find(this.currentPage)
      },

      handleCurrentChange(val) {
        this.currentPage = val;
        //console.log(val)
        if (this.state == 1) {
          this.find(this.currentPage)
        } else this.find(this.currentPage)
      },
    }
  }
</script>
